package com.ebt.m.proposal_v2.voice.view;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.v4.app.FragmentManager;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.proposal_v2.bean.AudioTap;
import com.ebt.m.proposal_v2.helper.ThemeHelper;
import com.ebt.m.proposal_v2.utils.RxUtils;
import com.ebt.m.proposal_v2.voice.TapApi;
import com.ebt.m.proposal_v2.widget.base.CompatLinearLayout;
import com.ebt.m.proposal_v2.widget.dialog.EBTConfirmDialog;

import butterknife.BindView;

/**
 * 音频View
 *
 * @author archer.qi
 *         Created on 2017/5/19.
 */
public class AudioView extends CompatLinearLayout {

    @BindView(R.id.audio_body)
    LinearLayout mAudioBody;
    @BindView(R.id.audio_close)
    ImageView mAudioDelete;

    @BindView(R.id.audio_play_logo)
    ImageView mAudioPlayLogo;
    @BindView(R.id.audio_duration)
    TextView mAudioDuration;

    private AudioTap mAudio;
    private boolean isPlaying;
    private FragmentManager fragmentManager;
    private OnTapDeleteListener onTapDeleteListener;

    public AudioView(Context context) {
        super(context);
    }

    public AudioView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected int setContentView() {
        return R.layout.view_audio;
    }

    @Override
    protected void onBindView() {
        setPlayListener();
        setDeleteListener();
    }

    private void setPlayListener() {
        RxUtils.click(mAudioBody, view -> {
                    if (mAudio == null) {
                        throw new NullPointerException("media resource not found, please call setAudio() to give a valid AudioTap");
                    }
                    if (!isPlaying) {
                        isPlaying = true;
                        setFrameAnimation();
                        TapApi.getApi().playAudio(mAudio.path, mp -> stopPlayingAudio());
                    }
                }
        );
    }

    /**
     * 结束播放
     */
    private void stopPlayingAudio() {
        if (isPlaying) {
            TapApi.getApi().stopAudio();
            clearFrameAnimation();
            isPlaying = false;
        }
    }


    private void setFrameAnimation() {
        AnimationDrawable drawable = (AnimationDrawable) ThemeHelper.getDrawable(getContext(), R.drawable.frame_audio_play);
        mAudioPlayLogo.setImageDrawable(drawable);
        drawable.start();
    }

    private void clearFrameAnimation() {
        mAudioPlayLogo.clearAnimation();
        mAudioPlayLogo.setImageResource(R.drawable.ic_audio_play_3);
    }


    private void setDuration(long duration) {
        mAudioDuration.setText(duration + "\"");
    }

    private void setDeleteListener() {
        RxUtils.click(mAudioDelete, view -> deleteConfirm());
    }

    private void deleteConfirm() {
        EBTConfirmDialog dialog = new EBTConfirmDialog();
        dialog.setMessage("确认删除该条语音信息\n删除后可重新录制");
        dialog.show(fragmentManager, "deleteTap");
        dialog.setPositiveButton("删除", v -> {
            stopPlayingAudio();
            if (onTapDeleteListener != null) {
                onTapDeleteListener.onDelete(mAudio);
            }
            dialog.dismiss();
        });
        dialog.setNegativeButton("取消", v -> dialog.dismiss());
    }

    /**
     * 添加数据
     *
     * @param audio
     */
    public void setAudio(AudioTap audio) {
        this.mAudio = audio;
        setDuration(mAudio == null ? 0 : audio.duration);
    }

    /*必须设置*/
    public void setFragmentManager(FragmentManager manager) {
        fragmentManager = manager;
    }

    public void setOnDeleteListener(OnTapDeleteListener listener) {
        onTapDeleteListener = listener;
    }

    public interface OnTapDeleteListener {
        void onDelete(AudioTap tap);
    }


}
